<?php namespace system\model;
//use houdunwang\crypt\Crypt;此话不需要，加了反而会出错
use houdunwang\model\Model;


class User extends Model{
	//数据表
	protected $table = "user";

    /**
     * 登陆
     */
	public function login(){
          Validate::make([
             ['username','isnull','用户名不能为空',3],
              ['password','isnull','密码不能为空',3],
              //['code','captcha','验证码不正确',3],
          ]);
          //相当于select语句，账号密码都对的话$data会返回一个很长的对象，不对的话会打印出null
       if(!$data = User::where('username',$_POST['username'])->where('password',Crypt::encrypt($_POST['password']))->first()){
            //下面两行写法在用户名或密码写错的时候会显示Array，所以要改为另一种返回数组的形式
           //   $this->error = '账号或密码错误';
            //  return false;
           return ['effect'=>false,'message'=>'您的账号或密码错误'];
        }

        //对象转化为数组
        $data = $data->toArray();
        //存入session
          Session::set('uid',$data['uid']);//主键
          Session::set('username',$data['username']);
          return ['effect'=>true,'message'=>'登陆成功',];
      }
}